package cn.lrwjz.androidguide.ui.exmple;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.textfield.TextInputLayout;

import cn.lrwjz.androidguide.R;

public class SharedPreferencesActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_shared_preferences);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("SharedPreferences 示例");
        }

        TextInputLayout tfName = findViewById(R.id.tf_name);

        TextInputLayout tfPhone = findViewById(R.id.tf_phone);

        TextInputLayout tfEmail = findViewById(R.id.tf_email);

        // 从share.xml获取共享参数实例
        SharedPreferences shared = getSharedPreferences("share", MODE_PRIVATE);
        // 设置
        SharedPreferences.Editor editor = shared.edit(); // 获得编辑器的对象

        // 保存
        Button btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(v -> {
            if (("").equals(tfName.getEditText().getText().toString()) || ("").equals(tfEmail.getEditText().getText().toString()) || ("").equals(tfPhone.getEditText().getText().toString())) {
                Toast.makeText(this, "请输入完整信息", Toast.LENGTH_SHORT).show();
                return;
            }
            editor.putString("name", tfName.getEditText().getText().toString());
            editor.putString("email", tfEmail.getEditText().getText().toString());
            editor.putInt("phone", Integer.valueOf(tfPhone.getEditText().getText().toString()));

            editor.commit(); // 提交编辑器中的修改，同步提交
            // editor.apply(); 异步提交

            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            // 清空输入框
            tfName.getEditText().setText("");
            tfEmail.getEditText().setText("");
            tfPhone.getEditText().setText("");
        });

        // 获取
        Button btnGet = findViewById(R.id.btn_get);
        btnGet.setOnClickListener(v -> {
            String name = shared.getString("name", "");
            String email = shared.getString("email", "");
            int phone = shared.getInt("phone", 0);
            tfName.getEditText().setText(name);
            tfEmail.getEditText().setText(email);
            tfPhone.getEditText().setText(String.valueOf(phone));
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}